<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/4/28
 * Time: 17:18
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Floor as FloorModel;
use \app\common\model\Type as TypeModel;
use app\common\model\Goods as goodsModel;


class Type extends AdminBase{
    protected $floor_model;
    protected $type_model;
    protected $goods_model;


    protected function _initialize()
    {
        parent::_initialize();
        $this->floor_model = new FloorModel();
        $this->type_model = new TypeModel();
        $this->goods_model  = new GoodsModel();
        $category_level_list  = $this->floor_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
    }
    public function index()
    {
        $list = $this->type_model->index();
        return $this->fetch('index', ['list' => $list]);
    }

    public function add()
    {
        return $this->fetch('add');
    }

    /**
     * 保存种类
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Category');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['create_time'] = time();
                $data['update_time'] = time();
                if ($this->type_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 编辑种类
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $type = $this->type_model->find($id);
        return $this->fetch('edit', ['type' => $type]);
    }
    /**
     * 更新种类
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Category');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['update_time'] = time();
                if ($this->type_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除种类
     * @param $id
     */
    public function delete($id)
    {
        $article  = $this->goods_model->where(['tid' => $id])->find();
        if (!empty($article)) {
            $this->error('此种类下存在种类规格，不可删除');
        }
        if ($this->type_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    /**
     * 根据分类ID   查询该分类下面的种类
     */
    public function getTypeinfo($fid)
    {

        $list = $this->type_model->getTypeForFloor($fid);
        if (!$list) {
            return ['code' => 0];
        }
        return ['code' => 1, 'data' => $list];

    }
}